// pages/player/player.js
Page({
  data: {
    progress: 0,
    isPlaying: false,
    playStatus: 'paused', // 初始化播放状态
    currentEpisode: { url: '' },// 初始化音频URL
    playbackRate: 1.0
  },
  
  onLoad() {
    this.audioCtx = wx.createInnerAudioContext()
    this.audioCtx.onTimeUpdate(() => {
      this.setData({
        progress: (this.audioCtx.currentTime / this.audioCtx.duration * 100).toFixed(2)
      })
    })
  },
  
  togglePlay() {
    const app = getApp()
    const audioContext = app.globalData.audioContext
    if (this.data.playStatus === 'playing') {
      audioContext.pause();
      this.setData({ playStatus: 'paused' })
    }else{
      if (!audioContext.src && this.data.currentEpisode.url) {
        audioContext.src = this.data.currentEpisode.url
      }
      audioContext.play()
      this.setData({ playStatus: 'playing' })
    }
  },
  
  changePlaybackRate() {
    const rates = [1.0, 1.25, 1.5, 2.0]
    const newRate = rates[(rates.indexOf(this.data.playbackRate) + 1) % rates.length]
    this.audioCtx.playbackRate = newRate
    this.setData({ playbackRate: newRate })
  }
})
    